<!DOCTYPE HTML>
<html>
<head>
<title>FileEncoding | AutoHotkey</title>
<meta name="description" content="The FileEncoding command sets the default encoding for FileRead, FileReadLine, Loop Read, FileAppend, and FileOpen()." />
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>FileEncoding <span class="ver">[AHK_L 42+]</span></h1>

<p>为 <a href="FileRead.htm">FileRead</a>, <a href="FileReadLine.htm">FileReadLine</a>, <a href="LoopReadFile.htm">Loop Read</a>, <a href="FileAppend.htm">FileAppend</a> 和 <a href="FileOpen.htm">FileOpen()</a> 设置默认编码.</p>

<pre class="Syntax"><span class="func">FileEncoding</span> <span class="optional">, Encoding</span></pre>

<h2 id="Parameters">参数</h2>
<dl>
  <dt>Encoding</dt>
  <dd>
    <p>以下值之一(如果省略, 它默认为系统默认的 ANSI 代码页, 这也是默认设置):</p>
    <p><strong>UTF-8:</strong> Unicode UTF-8, 等同于 CP65001.</li>
    <p><strong>UTF-8-RAW:</strong> 像上面一样, 但创建新文件时不写入字节顺序标记.</li>
    <p><strong>UTF-16:</strong> Unicode UTF-16 带小端字节顺序标识, 等同于 CP1200.</p>
    <p><strong>UTF-16-RAW:</strong> 像上面一样, 但创建新文件时不写入字节顺序标记.</p>
    <p><strong>CP<em>nnn</em>:</strong> 带数值标识符 <em>nnn</em> 的代码页. 请参阅<a href="https://learn.microsoft.com/windows/desktop/Intl/code-page-identifiers">代码页标识符</a>.</p>
  </dd>
</dl>

<h2 id="Remarks">备注</h2>
<p>内置变量 <strong>A_FileEncoding</strong> 包含了当前的默认编码设置.</p>
<p>每个新启动的<a href="../misc/Threads.htm">线程</a>(如<a href="../Hotkeys.htm">热键</a>, <a href="Menu.htm">自定义菜单项</a>或<a href="SetTimer.htm">计时器</a>子例程) 都使用此命令的默认设置重新启动. 可以在自动执行段(脚本的顶部) 中使用此命令来更改该缺省值.</p>
<p>如果文件中存在 UTF-8 或 UTF-16 字节顺序标识, 则不会使用默认编码, 除非文件以只写访问打开(即文件的先前内容被丢弃).</p>

<h2 id="Related">相关</h2>
<p><a href="FileOpen.htm">FileOpen()</a>, <a href="StrGet.htm">StrGet</a>, <a href="StrPut.htm">StrPut()</a>, <a href="../Compat.htm#FileRead">脚本兼容性</a></p>

</body>
</html>